package com.sl.diary.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.sl.diary.R;
import com.sl.diary.adapter.ItineraryItemAdapter;
import com.sl.diary.callback.AlertDialogCallBack;
import com.sl.diary.component.ListPopup;
import com.sl.diary.component.ListPopup.PopupItemAdapterEventCallback;
import com.sl.diary.db.TravelSQLiteHelper;
import com.sl.diary.model.TourModel;
import com.sl.diary.model.TravelModel;
import com.sl.diary.util.Constant;
import com.sl.diary.util.Util;

public class A004_TravelDetail extends BaseActivity implements
		PopupItemAdapterEventCallback, AlertDialogCallBack, OnTouchListener {

	TravelModel travel;
	List<TourModel> tours = new ArrayList<TourModel>();

	LinearLayout lt_information, lt_option;
	ImageView img_option_scroll;
	String TAG = "A004_TravelDetail";
	int width, height, option_tab_height;
	float previous_y;

	ListPopup list_popup;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a004_travel_detail);
		this.initView();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (data != null) {
			Bundle bundle = data.getExtras();
			if (bundle.getInt(Constant.TRAVEL_STATE) == Constant.TRAVEL_STATE_EDIT
					&& resultCode == Constant.A005_CODE) {
				initView();
			}
		}
	}

	private void initView() {

		super.initNavigationBar();

		Bundle extras = getIntent().getExtras();
		if (extras != null && extras.getString("travel_id") != null) {
			String travel_id = extras.getString("travel_id");
			travel = dbManager.getTravel(travel_id);

			/*
			 * navigation bar
			 */
			ImageButton imgbtn_menu = (ImageButton) findViewById(R.id.imgbtn_navigation_menu);
			imgbtn_menu.setOnClickListener(onClickMenuBtn);

			/*
			 * travel detail information
			 */
			lt_information = (LinearLayout) findViewById(R.id.lt_travel_detail_information);
			// travel name
			TextView tv_name = (TextView) lt_information
					.findViewById(R.id.tv_travel_detail_name);
			tv_name.setText(travel.getName());

			// travel duration
			TextView tv_duration = (TextView) lt_information
					.findViewById(R.id.tv_travel_detail_duration);
			tv_duration.setText(this.getTravelDuration(
					travel.getDeparture_date(), travel.getEnd_date()));

			// travel description
			TextView tv_description = (TextView) lt_information
					.findViewById(R.id.tv_travel_detail_desciption);
			tv_description.setText(travel.getDescription());

			/*
			 * travel detail option
			 */
			Display display = getWindowManager().getDefaultDisplay();
			DisplayMetrics outMetrics = new DisplayMetrics();
			display.getMetrics(outMetrics);
			Point size = new Point();
			/*
			 * if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			 * display.getSize(size);
			 * 
			 * width = size.x; height = size.y; } else {
			 */
			width = display.getWidth();
			height = display.getHeight();
			// }
			Log.e(TAG, "width_screen: " + width);
			Log.e(TAG, "height_screen: " + height);

			lt_option = (LinearLayout) findViewById(R.id.lt_travel_detail_option);
			lt_option.setLayoutParams(new LinearLayout.LayoutParams(width,
					height / 2));

			LinearLayout lt_option_tab = (LinearLayout) lt_option
					.findViewById(R.id.lt_travel_option_tab);

			img_option_scroll = (ImageView) lt_option
					.findViewById(R.id.img_travel_option_scroll);
			img_option_scroll.setOnTouchListener(this);

			option_tab_height = lt_option_tab.getHeight()
					+ img_option_scroll.getHeight();

			// set itinerary
			if (dbManager != null) {
				tours = dbManager.getAllTourAsTravel(travel_id);

				ListView lv_itinerary = (ListView) findViewById(R.id.lv_travel_detail_itinerary);
				ItineraryItemAdapter adapter = new ItineraryItemAdapter(
						this.getApplicationContext(),
						R.id.lv_travel_detail_itinerary, tours);
				lv_itinerary.setAdapter(adapter);
				lv_itinerary.setOnItemClickListener(onItineraryItemClick);
			}
		}
	}

	private String getTravelDuration(Date departureDate, Date endDate) {
		String duration = "From: ";
		duration += Util.convertDateToString(departureDate);
		duration += " - To: ";
		duration += Util.convertDateToString(endDate);
		return duration;
	}

	OnClickListener onClickMenuBtn = new OnClickListener() {

		@Override
		public void onClick(View view) {
			String[] menu = { "Edit", "Delete" };
			if (list_popup == null) {
				list_popup = new ListPopup(A004_TravelDetail.this);
				list_popup.setData(menu, A004_TravelDetail.this);
			}
			list_popup.showAsDropDown(view, -50, 0);
		}
	};

	/**
	 * Implement PopupItemAdapterEventCallback - choose menu option (edit/delete
	 * travel detail)
	 * 
	 * @param view
	 *            the view
	 */
	@Override
	public void onSelectPopupItem(int position) {
		switch (position) {
		case 0:
			// edit travel
			Intent intent = new Intent(A004_TravelDetail.this,
					A005_RegisterTravel.class);
			intent.putExtra(TravelSQLiteHelper.TRAVEL_ID, travel.getId());
			startActivityForResult(intent, Constant.A004_CODE);
			break;

		case 1:
			// delete travel
			showDeleteTravelAlert(A004_TravelDetail.this, travel,
					A004_TravelDetail.this);
			break;

		default:
			break;
		}

		// close listPopup
		if (list_popup != null && list_popup.isShowing()) {
			list_popup.dismiss();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sl.diary.callback.AlertDialogCallBack#onAlertOKCallBack()
	 */
	@Override
	public void onAlertOKCallBack() {
		dbManager.deleteTravel(travel.getId());
		Intent data = new Intent();
		data.putExtra(Constant.TRAVEL_STATE, Constant.TRAVEL_STATE_DEL);
		setResult(Constant.A004_CODE, data);
		finish();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sl.diary.callback.AlertDialogCallBack#onAlertCancelCallBack()
	 */
	@Override
	public void onAlertCancelCallBack() {
		// TODO Auto-generated method stub

	}

	/**
	 * Implement OnTouchListener
	 * 
	 * @param view
	 *            the View
	 * @param event
	 *            the MotionEvent
	 */
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		float eventY = event.getRawY();
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			Log.e(TAG, "ACTION_DOWN: " + eventY);
			previous_y = eventY;
			break;
		case MotionEvent.ACTION_UP:
			Log.e(TAG, "ACTION_UP: " + eventY);
			break;
		case MotionEvent.ACTION_MOVE:
			Log.e(TAG, "ACTION_MOVE: " + eventY);
			float y = event.getRawY();
			float diff = y - previous_y;
			Log.e(TAG,
					"ACTION_MOVE layout.getTop: " + img_option_scroll.getTop());
			Log.e(TAG, "ACTION_MOVE diff: " + diff);
			previous_y = y;

			if (diff > 0) {
				// pull down
				img_option_scroll.offsetTopAndBottom((int) (img_option_scroll
						.getHeight()));
				lt_option.setLayoutParams(new LinearLayout.LayoutParams(width,
						img_option_scroll.getTop()));
			} else {
				// pull up
				lt_option.setLayoutParams(new LinearLayout.LayoutParams(width,
						height / 2));
			}

			break;
		default:
			break;
		}
		return true;
	}

	/**
	 * OnItemClickListener - show map detail
	 */
	OnItemClickListener onItineraryItemClick = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View v, int position,
				long id) {
			Intent intent = new Intent(A004_TravelDetail.this,
					A006_MapDetail.class);
			startActivity(intent);
		}
	};
}
